package com.banciyuan.bcywebview.biz.main.mineinfo.drafts;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.Collections;
import java.util.List;

import de.greenrobot.daoexample.model.DraftsPostItem;

/**
 * Created by hy on 2016/4/5.
 */
public class DraftsActivity extends BaseActivity {
    private ListView mListView;
    private View viewEmpty;
    public static int GO_EDIT_DRAFTS = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drafts_layout);
        initActionbar();
        initUi();
        initData();
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.my_drafts));
    }

    @Override
    protected void initUi() {
        mListView = (ListView) findViewById(R.id.lv);
        viewEmpty = findViewById(R.id.rl_empty_view);
    }

    @Override
    protected void initData() {
        String uid = UserDataHelper.getInstance(this).getUid();
        String novelDraftsStr = SPHelper.getString(this, SPConstant.SPNAME_NOVELDRAFTS + uid, SPConstant.NOVEL, "[]");
        Gson gson = new Gson();
        List<DraftsPostItem> novelDrafts = gson.fromJson(novelDraftsStr, new TypeToken<List<DraftsPostItem>>() {
        }.getType());
        if (null == novelDrafts || novelDrafts.size() == 0) {
            viewEmpty.setVisibility(View.VISIBLE);
        } else {
            viewEmpty.setVisibility(View.GONE);
            Collections.sort(novelDrafts);
            DraftsAdapter mDraftsAdapter = new DraftsAdapter(this, novelDrafts);
            mListView.setAdapter(mDraftsAdapter);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GO_EDIT_DRAFTS && resultCode == RESULT_OK)
            initData();
    }
}
